﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using Tizer.Core;
using Tizer.Core.Repository;
using Tizer.Web.Backoffice.Controls;

namespace Tizer.Web.Backoffice
{
    /// <summary>
    /// Добавлени города
    /// </summary>
    public partial class CityAdd : AdminBasePage
    {
        /// <summary>
        /// Инициализация страницы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Init(object sender, EventArgs e)
        {
            BindCountryList();
        }

        /// <summary>
        /// Загрузка страницы
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            this.SetPageTitle();
        }

        /// <summary>
        /// Добавить город
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Add(object sender, EventArgs e)
        {
            bool flagSaved = false;

            if (Page.IsPostBack && Page.IsValid)
            {
                if (ddlCountry.SelectedItem.Value != "0")
                {
                    if (!string.IsNullOrEmpty(txtName.Text))
                    {
                        using (var cityRepository = new CityRepository())
                        {
                            var city = new City
                                           {
                                               Name = txtName.Text,
                                               CountryID = Convert.ToInt32(ddlCountry.SelectedItem.Value)
                                           };

                            cityRepository.Add(city);
                            cityRepository.SaveChanges();

                            tizerMessage.SetMessage("Сообщение", "Можете добавить следующуй");
                            flagSaved = true;
                        }
                    }
                }
                else
                {
                    tizerMessage.SetMessage("Внимание", "Выберите вначале страну", MessageType.Warning);
                    flagSaved = true;
                }
            }
            if (!flagSaved)
            {
                tizerMessage.SetMessage("Ошибка", "Невозможно добавить город", MessageType.Error);
            }
            txtName.Text = "";
            txtName.Focus();
        }

        /// <summary>
        /// Заполнение списка стран
        /// </summary>
        private void BindCountryList()
        {
            var emptyItem = new ListItem { Text = StrNotChoosed, Value = "0" };
            ddlCountry.Items.Add(emptyItem);

            using (var countryRepository = new CountryRepository())
            {
                var countries = countryRepository.GetAll();
                if (countries != null)
                {
                    foreach (var country in countries)
                    {
                        var item = new ListItem { Text = country.Name, Value = country.ID.ToString() };
                        ddlCountry.Items.Add(item);
                    }
                }
            }
        }
    }
}